package pw.goyd.game.api;

import pw.goyd.game.constant.setting.LuckyPool;
import pw.goyd.game.model.LuckyAward;
import pw.goyd.game.server.Log;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 抽奖操作
 * Created by StanWind on 16/11/5.
 */
public class LuckyPoolImp implements LuckyPool {
    private static List<LuckyAward> funs = new ArrayList<>();
    private static Random random;
    private static int count = 0;   //  总的权重

    public static void init() {
        //金币部分
        funs.add(new LuckyAward(LuckyType.Gold, 300, 3));
        funs.add(new LuckyAward(LuckyType.Gold, 500, 3));
        funs.add(new LuckyAward(LuckyType.Gold, 800, 3));
        funs.add(new LuckyAward(LuckyType.Gold, 2000, 3));
        funs.add(new LuckyAward(LuckyType.Gold, 5000, 1));
        funs.add(new LuckyAward(LuckyType.Gold, 10000, 1));
        //钻石部分
        funs.add(new LuckyAward(LuckyType.Diamond, 2, 3));
        funs.add(new LuckyAward(LuckyType.Diamond, 3, 3));
        funs.add(new LuckyAward(LuckyType.Diamond, 5, 3));
        funs.add(new LuckyAward(LuckyType.Diamond, 10, 2));
        //再来一次
        funs.add(new LuckyAward(LuckyType.Again, 3));
        funs.add(new LuckyAward(LuckyType.None, 2));

        //列表初始化
        random = new Random();
        for (LuckyAward l : funs)
            count += l.getWeight();
        Log.getLogger().info("\n-－－－－－－－－－－-－－－－大转盘奖品－－－－－-－－－－－－－－－－\n" + funs);
    }

    /**
     * 大转盘
     *
     * @return 奖励物品对象
     */
    public static LuckyAward getRandAward() {
        int ran = random.nextInt(count) + 1;    //  在总权重中取随机数
        int index = searchIndex(ran);           //  取奖励物品对象下标
        return funs.get(index);
    }

    /**
     * 根据总权重 和 积分寻找下标
     *
     * @return 目标对象下标
     */
    private static int searchIndex(int a) {
        int temp = a;
        int i;
        for (i = 0; i < funs.size(); i++) {
            temp -= funs.get(i).getWeight();
            if (temp <= 0)
                break;
        }
        return i;
    }
}
